events { }

http {
  include mime.types;
  # See https://docs.galaxyproject.org/en/latest/admin/nginx.html#serving-galaxy-at-the-web-server-root

  # compress responses whenever possible
  gzip on;
  gzip_http_version 1.1;
  gzip_vary on;
  gzip_comp_level 6;
  gzip_proxied any;
  gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
  gzip_buffers 16 8k;

  # allow up to 3 minutes for Galaxy to respond to slow requests before timing out
  uwsgi_read_timeout {{ NGINX_UWSGI_READ_TIMEOUT | default(180, true) }};

  # maximum file upload size
  client_max_body_size 10g;

  server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name _;

    # use a variable for convenience
    set $galaxy_static /export/galaxy/static;
    set $galaxy_root /export/galaxy;

    # proxy all requests not matching other locations to uWSGI
    location /{{ GALAXY_PROXY_PREFIX | regex_replace("^/", "") | regex_replace("/$", "") }} {
      uwsgi_pass galaxy-server:5555;
      include uwsgi_params;
      uwsgi_param UWSGI_SCHEME /{{ GALAXY_PROXY_PREFIX | regex_replace("^/", "") | regex_replace("/$", "") }};
    }

    # serve framework static content
    location {{ GALAXY_PROXY_PREFIX | regex_replace("/$", "") }}/static {
      alias $galaxy_static;
      expires 24h;
    }
    location {{ GALAXY_PROXY_PREFIX | regex_replace("/$", "") }}/robots.txt {
      alias $galaxy_static/robots.txt;
      expires 24h;
    }
    location {{ GALAXY_PROXY_PREFIX | regex_replace("/$", "") }}/favicon.ico {
      alias $galaxy_static/favicon.ico;
      expires 24h;
    }

    # serve visualization and interactive environment plugin static content
    location ~ ^{{ GALAXY_PROXY_PREFIX | regex_replace("/$", "") }}/plugins/(?<plug_type>.+?)/(?<vis_name>.+?)/static/(?<static_file>.*?)$ {
      alias $galaxy_root/config/plugins/$plug_type/$vis_name/static/$static_file;
      expires 24;
    }

    rewrite ^/{{ GALAXY_PROXY_PREFIX | regex_replace("^/", "") | regex_replace("/$", "") }}$ /{{ GALAXY_PROXY_PREFIX | regex_replace("^/", "") | regex_replace("/$", "") }}/ last;
  }
}
